package com.dog.view.controller;

import com.dog.model.ContactPerson;
import com.dog.model.Sex;
import com.dog.service.IContactPersonService;
import com.dog.spring.ServiceFactory;
import com.dog.view.beans.BaseBean;
import com.dog.view.beans.ContactPersonBean;
import com.dog.view.utils.EnumSelectItems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;
import java.util.List;

/**
 * User: Kuttubek Aidaraliev
 * Date: 06.04.2010
 * Time: 13:35:27
 */
@Controller
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class ContactPersonBeanController extends BaseBeanController<IContactPersonService, ContactPerson, ContactPersonBean> {

    @Autowired
    private IContactPersonService contactPersonService;
    private boolean returnNull = false;

    public ContactPersonBeanController() {
    }

    @PostConstruct
    protected void initialized(){
       contactPersonService = ServiceFactory.getContactPersonService();
       setService(contactPersonService);
    }

    public String cancel(){
        String target = super.preInsert();
        setEditField(false);

        if(returnNull){
            returnNull = false;
            return null;
        }
        else return target;
    }

    @Override
    public String preInsert(){
        String target = super.preInsert();
        if(returnNull){
            returnNull = false;
            return null;
        }
        else return target;
    }

    @Override
    public String preUpdate(){
       ContactPerson contactPerson = getService().getByIdWithRoles(getEntity().getId());
       setEntity(contactPerson);
       String target = super.preUpdate();
       System.out.println(target + " " + returnNull);
       if(returnNull){
           returnNull = false;
           return null;
       }
       return target;
    }

    @Override
    public String inserted(){
        String target = super.inserted();
        System.out.println(target + " " + returnNull);
        if(returnNull) {
            returnNull = false;
            return null;
        }
        return target;
    }

    @Override
    public String updated(){
        String target = super.updated();
        if(returnNull) {
            returnNull = false;
            return null;
        }
        return target;
    }

    @Override
    public String list() {
        return "contactPersonList";  
    }

    @Override
    public String deleted(){
        String target = super.deleted();
        System.out.println(target + " " + returnNull);
        if(returnNull){
            returnNull = false;
            return null;
        }
        else return target;
    }

    @Override
    protected String form() {
        return "contactPerson";
    }

    @Override
    protected ContactPerson newEntity() {
        return new ContactPerson();
    }

    @Override
    protected SelectItem createSelectItem(ContactPerson contactPerson) {
        return new SelectItem(contactPerson, contactPerson.getFullName());  
    }

    @Override
    protected String getNameEntity() {
        return "contactPersonBean";
    }

    @Override
    protected ContactPersonBean createEntityBean() {
        return new ContactPersonBean();
    }

    public List<SelectItem> getSexSelectItems() {
        return EnumSelectItems.getSelectItems(Sex.class);
    }

    public String preView(){
        ContactPerson contactPerson = getService().getByIdWithRoles(getEntity().getId());
        setEntity(contactPerson);
        return null;
    }

    public boolean isReturnNull() {
        return returnNull;
    }

    public void setReturnNull(boolean returnNull) {
        this.returnNull = returnNull;
    }

}